package com.wn.controller;


import com.wn.entity.Manager;
import com.wn.entity.ManagerDTO;
import com.wn.entity.ResponseEntity;
import com.wn.service.ManagerService;
import com.wn.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class LoginController {
    @Autowired
    ManagerService managerService;

    @RequestMapping("/login")
    public ResponseEntity login(String username, String password) {
        Manager manager = managerService.login(username, password);
        if (!StringUtils.isEmpty(manager)) {
            //就说明查出了用户数据
            String token = JWTUtils.sign(username, password);
            ManagerDTO managerDTO = new ManagerDTO();
            managerDTO.setAccount(manager.getAccount());
            managerDTO.setId(manager.getId());
            managerDTO.setPassword(manager.getPassword());
            managerDTO.setRole_id(manager.getRole_id());
            managerDTO.setRoles(manager.getRoles());
            managerDTO.setStatus(manager.getStatus());
            managerDTO.setToken(token);
            return new ResponseEntity<>(200, "登录成功", managerDTO);
        } else {
            return ResponseEntity.fail;
        }
    }

    @RequestMapping("/401")
    public ResponseEntity response401() {
        return new ResponseEntity(401, "认证异常！", null);
    }
}
